import 'package:flutter/cupertino.dart';
import 'package:get/get.dart';
import 'package:wallet_utils/timer_count_down.dart';
import 'package:wallet_utils/toast.dart';

import 'state.dart';

class ForgetPwdLogic extends GetxController {
  final ForgetPwdState state = ForgetPwdState();
  late TimerCountDownWidget timerCountDownWidget;
  @override
  void onInit() {
    super.onInit();
    timerCountDownWidget = TimerCountDownWidget(
      isOnTap: state.mobile.length >= 11,
      onTimerFinish: () {},
      onTap:  ()  async {
        if(state.isAgreeUserPrivary){
          return  true;
        }else{
          ToastUtils.showToast('请勾选并阅读协议');
          return false;
        }

      },
      onClickGetSmsCode: () {
        //获取验证码
        // getSmsCode();
      },
      isTextColor: state.mobile.length >= 11,
    );
    state.timerCountDownWidget = timerCountDownWidget;
  }
  //获取账号密码输入框焦点
  void requestAccountFocus(FocusNode mobileFocusNode, FocusNode pwdFocusNode) {
    mobileFocusNode.addListener(() {
      if (!mobileFocusNode.hasFocus) {
        //失去焦点
        state.isAccountFocus = false;
      } else {
        //获得焦点
        state.isAccountFocus = true;
      }
    });

    pwdFocusNode.addListener(() {
      if (!pwdFocusNode.hasFocus) {
        //失去焦点
        state.isPwdFocus = false;
      } else {
        //获得焦点
        state.isPwdFocus = true;
      }
    });
    update();
  }
  //判断是否可以登录
  void allowLogin(TextEditingController mobileController, TextEditingController pwdController) {
    state.mobile = mobileController.text;
    state.pwd = pwdController.text;
    if (state.mobile.isNotEmpty && state.pwd.isNotEmpty) {
      state.allowLogin = true;
    } else {
      state.allowLogin = false;
    }
    if (mobileController.text.isNotEmpty) {
      state.showDeleteIcon = true;
    } else {
      state.showDeleteIcon = false;
    }
    update();
  }

  deleteAccount() {
    state.mobileController.clear();
    state.showDeleteIcon = false;
    state.allowLogin = false;
    state.mobile = state.mobileController.text;
    //设置获取验证码颜色为灰色  并且不可点击
    state.timerCountDownWidget.changeTextColor(false);
    state.timerCountDownWidget.textIsTap(false);
    update();
  }
  //用户隐私协议选中或未选中
  void updateUserPrivary() {
    state.isAgreeUserPrivary = !state.isAgreeUserPrivary;
    update();
  }
}
